package com.example.performance.jol.official;

import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;

/**
 * @author Aleksey Shipilev
 */
@SuppressWarnings({"java:S101", "java:S106"})
public class JOLSample_03_Packing {

    /*
     * This is the example how VM packs the fields.
     *
     * JVMs pack the fields to minimize the memory footprint.
     * Run this example and see the fields are densely packed, and gaps are minimal.
     * It is achieved by aligning fields in 8->4->2->1 order,
     * because it can not break the initial alignment, once we align the 8-byte field.
     * The gap resulted in the initial 8-byte align can be taken by one or few smaller-sized fields.
     *
     * Note that the actual field order is very different from the declared order.
     * Nothing in the JVM spec requires otherwise.
     */

    public static void main(String[] args) {
        System.out.println(VM.current().details());
        System.out.println(ClassLayout.parseClass(A.class).toPrintable());
    }

    @SuppressWarnings("unused")
    public static class A {
        boolean bo1;
        boolean bo2;
        byte b1;
        byte b2;
        char c1;
        char c2;
        double d1;
        double d2;
        int i1;
        int i2;
        float f1;
        float f2;
        long l1;
        long l2;
        short s1;
        short s2;
    }

}